package com.example.admin.yuejian21.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.admin.yuejian21.Bean.Reg;
import com.example.admin.yuejian21.Model.UserModel;
import com.example.admin.yuejian21.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegActivity extends AppCompatActivity {
    private EditText tel,password;
    private Button reg;
    private Button h_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initViews();
        initEvents();

    }
    public void initEvents() {
        h_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(RegActivity.this,LoginActivity.class);
                startActivity(intent1);
            }
        });
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String str_psd =password.getText().toString();

                String str_tel=tel.getText().toString();


                if(str_tel.equals("") || str_psd.equals("") ){
                    Toast.makeText(RegActivity.this, "您有的信息还未填写", Toast.LENGTH_SHORT).show();
                }else{
//                    if(str_psd.equals(str_psd)){
                        UserModel userModel = new UserModel();
                         Call<Reg> call = userModel.reg(str_tel,str_psd);
                        //3. 发送请求
                        call.enqueue(new Callback<Reg>(){
                            @Override
                            public void onFailure(Call<Reg> user, Throwable throwable) {
                                Toast.makeText(RegActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                            @Override
                            public void onResponse(Call<Reg> user, Response<Reg> response) {


                                    Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent();
                                    String str_tel=tel.getText().toString();
                                    intent.putExtra("tel",str_tel);
                                    setResult(RESULT_OK,intent);
                                    finish();
                                }


                        });
                    }


            }
        });
    }

    private void initViews() {

        password= (EditText) findViewById(R.id.editText2);


         tel= (EditText) findViewById(R.id.editText);

        reg= (Button) findViewById(R.id.button2);
        h_return= (Button) findViewById(R.id.button);
    }

}

